package org.xdq.新的两种线程的创建方式;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test2 {
    public static void main(String[] args) {


        ExecutorService executorService =

                //带缓存的线程池
                Executors.newCachedThreadPool();

                //创建可单线程的线程池
                //Executors.newSingleThreadExecutor();

                //创建可容纳10个线程的线程池（固定大小）
                Executors.newFixedThreadPool(10);

        for (int k=0;k<20;k++) {
            //提交并执行线程
            executorService.submit(()->{
                for(int i=0;i<100;i++){
                    System.out.println(Thread.currentThread().getName()+"=>"+i);
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        executorService.shutdown();//停用线程池，但是已在线程池的线程正常执行


    }
}
